package com.jeeplus.modules.graph.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.graph.entity.*;
import com.jeeplus.modules.graph.service.GraphService;
import com.jeeplus.modules.studentevaluate.entity.StudentEvaluate;
import com.jeeplus.modules.sys.entity.StudentMoreduMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.Map.Entry;

/**
 * 图表Controler
 * Created by 于昭龙 on 2022/5/6.
 */
@RestController
@RequestMapping(value = "/graph/Graph")
public class GraphControler extends BaseController {
    @Autowired
    private GraphService graphService;

    @PostMapping("getPointOrEveragePointUI")
    public AjaxJson getPointOrEveragePoint(StudentMoreduMain studentMoreduMain,
                                           @RequestParam("ids") String ids,
                                           @RequestParam("flag") String flag){
        Map<String,Double> map = graphService.getPointOrEveragePoint(studentMoreduMain,ids,flag);

        List<GraphPairData> data = new ArrayList<>();
        Set<Entry<String,Double>> entrySet = map.entrySet();
        for (Entry<String,Double> entry :entrySet){
            data.add(new GraphPairDataUI( entry.getKey(), entry.getValue()));
        }
        RingGraphData da = new RingGraphData(data);
        //传输
        return AjaxJson.success().put("GraphData",da);
    }

    /**
     * 微信端查询汇总图，平均图的封装
     * @param studentMoreduMain 主要参数，但其中有用的只有 系、年级、有用
     * @param ids 当前office的路径
     * @param flag 目前"everage" 为平均数
     * @return
     */
    @PostMapping("getPointOrEveragePointPhone")
    public AjaxJson getPointOrEveragePointPhone(StudentMoreduMain studentMoreduMain,
                                                @RequestParam("ids") String ids,
                                                @RequestParam("flag") String flag){
        Map<String,Double> map = graphService.getPointOrEveragePoint(studentMoreduMain,ids,flag);

        List<GraphPairDataPhone> data = new ArrayList<>();
        Set<Entry<String,Double>> entrySet = map.entrySet();
        for (Entry<String,Double> entry :entrySet){
            data.add(new GraphPairDataPhone( entry.getKey(), entry.getValue()));
        }
        RingGraphData da = new RingGraphData(data);
        //传输
        return AjaxJson.success().put("GraphData",da);
    }

    @PostMapping("getStudentsFromOffice")
    public AjaxJson getStudentsFromOffice(StudentMoreduMain studentMoreduMain){
        List<StudentMoreduMain> list= graphService.getStudentsFromOffice(studentMoreduMain);
        return AjaxJson.success().put("list",list);
    }

    /**
     * 查询评价情况
     * @param evaluation
     * @return
     */
//    @PostMapping("isEvaluate")
//    public AjaxJson isEvaluate(@RequestParam("ids") String ids, Evaluation evaluation){
//        List<Evaluation> list = graphService.isEvaluate(ids,evaluation);
//        return AjaxJson.success().put("list",list);
//    }

    // 按学院的评价情况
    @PostMapping("organizatioEvaluationSituation")
    public AjaxJson organizatioEvaluationSituation(Evaluation evaluation) {
        List<Evaluation> list = graphService.organizatioEvaluationSituation(evaluation);
        return AjaxJson.success().put("list", list);
    }

    // 按学院、年级分组的评价情况
    @PostMapping("evaluationSituation")
    public AjaxJson evaluationSituation(Evaluation evaluation) {
        List<Evaluation> list = graphService.evaluationSituation(evaluation);
        return AjaxJson.success().put("list", list);
    }

    // 所有学院的评价情况
    @PostMapping("allEvaluationSituation")
    public AjaxJson allEvaluationSituation(Evaluation evaluation) {
        List<Evaluation> list = graphService.allEvaluationSituation(evaluation);
        return AjaxJson.success().put("list", list);
    }
    /**
     * 通过机构查询
     */
    @PostMapping("byInstitute")
    public AjaxJson byInstitute(ByReason byReason){
        List<ByReason> list= graphService.byInstitute(byReason);
        return AjaxJson.success().put("list",list);
    }


    /**
     * 按照类别进行区分 返回平均加分数和总分
     */
    @PostMapping("byReason")
    public AjaxJson byReason(StudentMoreduMain studentMoreduMain){
        System.out.println(studentMoreduMain.toString());
        List<ByReason> list= graphService.byReason(studentMoreduMain);
        return AjaxJson.success().put("list",list);
    }

    /**
     * 按照类别进行区分 返回总分的学院对比图
     * @param byReason
     * @return
     */
    @PostMapping("byInstituteContrast")
    public AjaxJson byInstituteContrast(ByReason byReason){
        List<ByReason> list= graphService.byInstituteContrast(byReason);
        HashMap<String,List<ByReason>> hm = new HashMap<>();
        List<String> reasonList = new ArrayList<>(); // 加分原因列表
        for(ByReason temp : list) {
            if(!reasonList.contains(temp.getAddReason())) { // 列表中没有该原因就加入
                reasonList.add(temp.getAddReason());
            }
        }
        for(int i =0; i < reasonList.size(); i++){
            List<ByReason> inlist = new ArrayList<>();  // 每循环一个原因，都重新定义inlist
            for(int j = 0; j < list.size(); j++){
                if(hm.containsKey(reasonList.get(i))) { // hashmap中有该key就直接加入
                    if(list.get(j).getAddReason().equals(reasonList.get(i))){
                        inlist.add(list.get(j));
                        hm.put(reasonList.get(i),inlist);
                    }
                }else {
                    hm.put(reasonList.get(i),null); // 没有就先放入该key
                    if(list.get(j).getAddReason() == reasonList.get(i)){
                        inlist.add(list.get(j));
                        hm.put(reasonList.get(i),inlist);
                    }
                }
            }
        }
        return AjaxJson.success().put("hashmap",hm);
    }

    /**
     * 按照类别进行区分 返回评价分的学院对比图
     * @param byReason
     * @return
     */
    @PostMapping("byInstituteContrastAve")
    public AjaxJson byInstituteContrastAve(ByReason byReason){
        List<ByReason> list= graphService.byInstituteContrastAve(byReason);
        HashMap<String,List<ByReason>> hm = new HashMap<>();
        List<String> reasonList = new ArrayList<>(); // 加分原因列表
        for(ByReason temp : list) {
            if(!reasonList.contains(temp.getAddReason())) { // 列表中没有该原因就加入
                reasonList.add(temp.getAddReason());
            }
        }
        for(int i =0; i < reasonList.size(); i++){
            List<ByReason> inlist = new ArrayList<>();  // 每循环一个原因，都重新定义inlist
            for(int j = 0; j < list.size(); j++){
                if(hm.containsKey(reasonList.get(i))) { // hashmap中有该key就直接加入
                    if(list.get(j).getAddReason().equals(reasonList.get(i))){
                        inlist.add(list.get(j));
                        hm.put(reasonList.get(i),inlist);
                    }
                }else {
                    hm.put(reasonList.get(i),null); // 没有就先放入该key
                    if(list.get(j).getAddReason() == reasonList.get(i)){
                        inlist.add(list.get(j));
                        hm.put(reasonList.get(i),inlist);
                    }
                }
            }
        }
        return AjaxJson.success().put("hashmap",hm);
    }

    @GetMapping("regional")
    public AjaxJson regional(StudentEvaluate studentEvaluate){
        List<GraphPairDataUI> list= graphService.regional(studentEvaluate);
        List<GraphPairDataUI> target = new ArrayList<>();
        for (GraphPairDataUI temp : list){
            if (temp.getName() == null)
                continue;
            if (temp.getName().equals("内蒙"))
                temp.setName("内蒙古");
            if (temp.getName().equals("黑龙"))
                temp.setName("黑龙江");
            target.add(temp);
        }
        return AjaxJson.success().put("list",target);
    }

    /**
     * 按照类别进行区分 返回总分的加分原因对比图
     * @param studentMoreduMain
     * @return
     */
    @PostMapping("byReasonContrast")
    public AjaxJson byReasonContrast(StudentMoreduMain studentMoreduMain){
        List<ByReason> list= graphService.byReasonContrast(studentMoreduMain);
        HashMap<String,List<ByReason>> hm = new HashMap<>();
        List<String> instituteList = new ArrayList<>(); // 学院列表
        for(ByReason temp : list) {
            if(!instituteList.contains(temp.getInstitute())) { // 列表中没有该学院就加入
                instituteList.add(temp.getInstitute());
            }
        }
        for(int i =0; i < instituteList.size(); i++){
            List<ByReason> inlist = new ArrayList<>();  // 每循环一个学院，都重新定义inlist
            for(int j = 0; j < list.size(); j++){
                if(hm.containsKey(instituteList.get(i))) { // hashmap中有该key就直接加入
                    if(list.get(j).getInstitute().equals(instituteList.get(i))){
                        inlist.add(list.get(j));
                        hm.put(instituteList.get(i),inlist);
                    }
                }else {
                    hm.put(instituteList.get(i),null); // 没有就先放入该key
                    if(list.get(j).getInstitute() == instituteList.get(i)){
                        inlist.add(list.get(j));
                        hm.put(instituteList.get(i),inlist);
                    }
                }
            }
        }
        return AjaxJson.success().put("hashmap",hm);
    }

    /**
     * 按照类别进行区分 返回平均分的加分原因对比图
     * @param studentMoreduMain
     * @return
     */
    @PostMapping("byReasonContrastAve")
    public AjaxJson byReasonContrastAve(StudentMoreduMain studentMoreduMain){
        List<ByReason> list= graphService.byReasonContrastAve(studentMoreduMain);
        HashMap<String,List<ByReason>> hm = new HashMap<>();
        List<String> instituteList = new ArrayList<>(); // 学院列表
        for(ByReason temp : list) {
            if(!instituteList.contains(temp.getInstitute())) { // 列表中没有该学院就加入
                instituteList.add(temp.getInstitute());
            }
        }
        for(int i =0; i < instituteList.size(); i++){
            List<ByReason> inlist = new ArrayList<>();  // 每循环一个学院，都重新定义inlist
            for(int j = 0; j < list.size(); j++){
                if(hm.containsKey(instituteList.get(i))) { // hashmap中有该key就直接加入
                    if(list.get(j).getInstitute().equals(instituteList.get(i))){
                        inlist.add(list.get(j));
                        hm.put(instituteList.get(i),inlist);
                    }
                }else {
                    hm.put(instituteList.get(i),null); // 没有就先放入该key
                    if(list.get(j).getInstitute() == instituteList.get(i)){
                        inlist.add(list.get(j));
                        hm.put(instituteList.get(i),inlist);
                    }
                }
            }
        }
        return AjaxJson.success().put("hashmap",hm);
    }
    /**
     *
     */
    @PostMapping("getGrade")
    public AjaxJson getGrade(){
        List<Choice> list= graphService.getGrade();
        return AjaxJson.success().put("list",list);
    }


    @GetMapping("queryRange")
    public AjaxJson queryRange(StudentMoreduMain studentMoreduMain){
        List<List<Integer>> lists = graphService.queryRange(studentMoreduMain);


        return AjaxJson.success().put("data",lists);
    }

}
